package com.zhangxin.threadcoreknowledge.stopthread;

/**
 * run方法中没有sleep或者wait方法，停止现场
 */
public class Demo01RightWayStopThreadWithoutSleep implements Runnable {
    @Override
    public void run() {
        // 第一版
//        int num = 0;
//        while (num <= Integer.MAX_VALUE / 2) {
//            if (num % 10000 == 0) {
//                System.out.println(num  + "是10000的倍数");
//            }
//            num++;
//        }
//        System.out.println("运行任务结束了");

        // 第二版
        int num = 0;
        while (!Thread.currentThread().isInterrupted() && num < Integer.MAX_VALUE / 2) {
            if (num % 10000 == 0 ) {
                System.out.println(num + "是10000的倍数");
            }
            num++;
        }
        System.out.println("运行任务结束啦");
    }

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new Demo01RightWayStopThreadWithoutSleep());
        thread.start();
        Thread.sleep(1000);
        thread.interrupt();
    }

}
